#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System.Collections.ObjectModel;

#endregion

namespace ZO.SmartCore.Windows.Forms
{
    /// <summary>
    /// Provides a collection container that enables wizard control to maintain a list of pages.
    /// </summary>
    public class SmartWizardPageCollection : Collection<SmartWizardPage>
    {

        #region Constructors

        #endregion

        #region Destructor

        #endregion

        #region Fields
        private SmartWizard _Parent;

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties

		/// <summary>
		/// Constructor requires the  wizard that owns this collection
		/// </summary>
		/// <param name="parent">Wizard</param>
        public SmartWizardPageCollection(SmartWizard parent)
            : base()
		{
			this.Parent = parent;
		}

		/// <summary>
		/// Returns the wizard that owns this collection
		/// </summary>
		public SmartWizard Parent
		{
			get 
			{
				return _Parent;
			}
            private set
            {
                this._Parent = value;
            }

		}
        #endregion

        #region Methods

        /// <summary>
        /// Adds an array of pages into the collection. Used by the Studio Designer generated coed
        /// </summary>
        /// <param name="pages">Array of pages to add</param>
        public void AddRange(SmartWizardPage[] pages)
        {
            // Use external to validate and add each entry
            foreach (SmartWizardPage page in pages)
            {
                this.Add(page);
            }
        }


        /// <summary>
        /// Inserts an element into the <see cref="T:System.Collections.ObjectModel.Collection`1"></see> at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert. The value can be null for reference types.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than zero.-or-index is greater than <see cref="P:System.Collections.ObjectModel.Collection`1.Count"></see>.</exception>
        protected override void InsertItem(int index, SmartWizardPage item)
        {
            base.InsertItem(index, item);

            if (this.Parent.SelectedPage == null)
            {
                this.Parent.SelectedPage = item;
            }
        }

        /// <summary>
        /// Removes the element at the specified index of the <see cref="T:System.Collections.ObjectModel.Collection`1"></see>.
        /// </summary>
        /// <param name="index">The zero-based index of the element to remove.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than zero.-or-index is equal to or greater than <see cref="P:System.Collections.ObjectModel.Collection`1.Count"></see>.</exception>
        protected override void RemoveItem(int index)
        {

            SmartWizardPage page = this[index];

            this.Parent.SelectedPage = FindPage(page);
            base.RemoveItem(index);
            this.Parent.Invalidate();

        }

        /// <summary>
        /// Finds the page.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <returns></returns>
        private SmartWizardPage FindPage(SmartWizardPage page)
        {
            if (this.Parent.SelectedPage == page)
            {
                if (page.PreviousPage != null)
                {
                    return page.PreviousPage;
                }
                else if (page.NextPage != null)
                {
                    return page.NextPage;
                }
                else
                {
                    foreach (SmartWizardPage var in this.Items)
                    {
                        if (var != page)
                        {
                            return var;
                        }
                    }
                }
            }

            return null;
        }

 
        #endregion
    }
}
